<?php  
namespace Admin\Controller;
use \Admin\Controller\IndexController;

	/**
	* @author denny <denny@mobnt.com>
	* 权限控制
	*/
class PrivilegeController extends IndexController
{
	//添加权限	
	public function add()
	{
		if (IS_POST) {
			$model = D('Admin/Privilege');
			if ($model->create(I('post.'),1)){
				if($id = $model->add()){
					$this->success('添加成功！',U('list?p='.I('get.p')));
					exit;
				}
			}
			$this->error($model->getError());
		}
		$parentModel = D('Admin/Privilege');
		$parentData = $parentModel->getTree();
		$this->assign('parentData',$parentData);

		$this->setPageBtn('添加权限','权限列表',U('list?p='.I('get.p')));
		$this->display();
	}

	//修改权限
	public function edit()
	{
		$id = I('get.id');
		if(IS_POST){
			$model = D('Admin/Privilege');
			if($model->create(I('post.'),2)){
				if ($model->save !== FALSE) {
					$this->success('修改成功！',U('list',array('p' => I('get.p',1))));
					exit;
				}
			}
			$this->error($model->getError());
		}
		$model = M('Privilege');
		$data = $model->find($id);
		$this->assign('data',$data);
		$parentModel = D('Admin/Privilege');
		$parentData = $parentModel->getTree();
		$children = $parentModel->getChildren($id);
			$this->assign(array(
			'parentData' => $parentData,
			'children' => $children,
		));

		$this->setPageBtn('修改权限', '权限列表', U('list?p='.I('get.p')));
		$this->display();
	} 

	//权限删除
	public function delete()
	{
		$model = D('Admin/Privilege');
		if ($model->delete(I('get.id',0)) !== FALSE){
			$this->success('删除成功！',U('list?p',array('p' => I('get.p',1))));
			exit;
		}
		else{
			$this->error($model->getError());
		}
	}
	
	//权限列表
	public function index()
	{
		$model = D('Admin/Privilege');
		$data = $model->getTree();
		$this->assign(array(
			'data' => $data,
			));
		$this->setPageBtn('权限列表', '添加权限', U('add'));
    	$this->display();
	}

}